package io.kimmking.dubbo.demo.consumer;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication(scanBasePackages = "io.kimmking.dubbo.demo,com.ylbl")
//@EnableEurekaClient
//@EnableDubbo
//@EnableFeignClients
public class DubboClientApplication {

//	@DubboReference(version = "1.0.0") //, url = "dubbo://127.0.0.1:12345")
//	private UserService userService;
//
//	@DubboReference(version = "1.0.0") //, url = "dubbo://127.0.0.1:12345")
//	private OrderService orderService;

	public static void main(String[] args) {

//		SpringApplication.run(DubboClientApplication.class).close();
		SpringApplication.run(DubboClientApplication.class, args);

		// UserService service = new xxx();
		// service.findById

//		UserService userService = Rpcfx.create(UserService.class, "http://localhost:8080/");
//		User user = userService.findById(1);
//		System.out.println("find user id=1 from server: " + user.getName());
//
//		OrderService orderService = Rpcfx.create(OrderService.class, "http://localhost:8080/");
//		Order order = orderService.findOrderById(1992129);
//		System.out.println(String.format("find order name=%s, amount=%f",order.getName(),order.getAmount()));

	}

	@Bean
	public ApplicationRunner runner() {
		return args -> {

			FlowRule flowRule = new FlowRule();
			//针对具体的方法限流,限流粒度可以是服务接口和服务方法两种粒度
			//若是限流方法粒度则resourceName为接口全限定名:方法签名
			flowRule.setResource("io.kimmking.dubbo.common.entity.UserService");
			flowRule.setCount(2);//限流阈值 qps=10
			flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);//限流阈值类型（QPS 或并发线程数）
			flowRule.setLimitApp("default");//流控针对的调用来源，若为 default 则不区分调用来源
			// 流量控制手段（直接拒绝、Warm Up、匀速排队）
			flowRule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);

			FlowRule flowRule_1 = new FlowRule();
			//针对具体的方法限流,限流粒度可以是服务接口和服务方法两种粒度
			//若是限流方法粒度则resourceName为接口全限定名:方法签名
			flowRule_1.setResource("io.kimmking.dubbo.common.entity.OrderService");
			flowRule_1.setCount(2);//限流阈值 qps=10
			flowRule_1.setGrade(RuleConstant.FLOW_GRADE_QPS);//限流阈值类型（QPS 或并发线程数）
			flowRule_1.setLimitApp("default");//流控针对的调用来源，若为 default 则不区分调用来源
			// 流量控制手段（直接拒绝、Warm Up、匀速排队）
			flowRule_1.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);

			List<FlowRule> frs = new ArrayList<>();
			frs.add(flowRule);
			frs.add(flowRule_1);
			FlowRuleManager.loadRules(frs);

//			for(int i=0;i<=6;i++) {
//				User user = userService.findById(1);
//				System.out.println("find user id=1 from server: " + user.getName());
//				Order order = orderService.findOrderById(1992129);
//				System.out.println(String.format("find order name=%s, amount=%f", order.getName(), order.getAmount()));
//			}
		};
	}

}
